package il.ac.technion.wiki.impl.tests;

import static org.junit.Assert.*;

import il.ac.technion.wiki.api.search.ISearchExpression;
import il.ac.technion.wiki.api.support.queries.QueryParser;
import il.ac.technion.wiki.api.support.queries.QueryParser.IllegalQueryException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestQueryParser {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testTokenize() throws IllegalQueryException {
		String s;
		
		s = QueryParser.tokenize("    pretty simple\t\r\n   this is").toString();
		assertEquals("{`pretty' `simple' `this' `is' } ", s);
		
		s = QueryParser.tokenize("WOAH (THERE NELLY) PHEW").toString();
		assertEquals("{`WOAH' {`THERE' `NELLY' }  `PHEW' } ", s);
		
		s = QueryParser.tokenize("\"This is () _ MUCH (HARDER)\" ISN'T it?").toString();
		assertEquals("{`\"This is () _ MUCH (HARDER)\"' `ISN'T' `it?' } ", s);
		
		s = QueryParser.tokenize("((((((NESTING))))))").toString();
		assertEquals("{`NESTING' } ", s);
	}

	@Test
	public void testParse() throws IllegalQueryException {
		ISearchExpression expr = QueryParser.parse("johnny cindy mandy");
		assertEquals("{{johnny} <--AND--> {cindy}} <--AND--> {mandy}", expr.toString());
		
		expr = QueryParser.parse("not me");
		assertEquals("~me", expr.toString());
		
		expr = QueryParser.parse("not me or not you");
		assertEquals("{~me} <--OR--> {~you}", expr.toString());
		
		expr = QueryParser.parse("\"you and i\" \"i and you\"");
		assertEquals("{\"you and i\"} <--AND--> {\"i and you\"}", expr.toString());
		
		expr = QueryParser.parse("(john or mandy and ben) not jenny not george");
		assertEquals("{{{{john} <--OR--> {mandy}} <--AND--> {ben}} <--AND--> {~jenny}} <--AND--> {~george}", expr.toString());
	}
}
